/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val)
{
    ListNode* phead, * ptail;
    phead = ptail = NULL;
    ListNode* pcur = head;
    while (pcur)
    {
        if (pcur->val != val)
        {
            if (!phead)
                phead = ptail = pcur;
            else
            {
                ptail->next = pcur;
                ptail = pcur;
            }
        }
        pcur = pcur->next;
    }
    if (ptail)
        ptail->next = NULL;
    return phead;
}